<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>配置更新</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 20px auto;
            padding: 0 20px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
        }
        input[type="text"] {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        .button-group {
            margin-top: 20px;
        }
        button {
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin-right: 10px;
        }
        button[type="submit"] {
            background-color: #4CAF50;
            color: white;
        }
        button[type="reset"] {
            background-color: #f44336;
            color: white;
        }
        .message {
            margin-top: 20px;
            padding: 10px;
            border-radius: 4px;
            display: none;
        }
        .success {
            background-color: #dff0d8;
            color: #3c763d;
            border: 1px solid #d6e9c6;
        }
        .error {
            background-color: #f2dede;
            color: #a94442;
            border: 1px solid #ebccd1;
        }
    </style>
</head>
<body>
    <h1>更新配置</h1>
    <form id="updateForm">
        <div class="form-group">
            <label for="url">Page URL</label>
            <input type="text" id="url" name="url" required>
        </div>
        <div class="form-group">
            <label for="logo">Logo URL</label>
            <input type="text" id="logo" name="logo" required>
        </div>
        <div class="form-group">
            <label for="title">Title</label>
            <input type="text" id="title" name="title" required>
        </div>
        <div class="button-group">
            <button type="submit">保存</button>
            <button type="reset">重置</button>
        </div>
    </form>
    <div id="message" class="message"></div>

    <script>
        document.getElementById('updateForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const url = document.getElementById('url').value;
            const logo = document.getElementById('logo').value;
            const title = document.getElementById('title').value;
            
            const content = `${url}\n${logo}\n${title}`;
            
            try {
                const response = await fetch('http://115.190.90.79:58001/api/upload', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({ content })
                });
                
                const result = await response.json();
                const messageDiv = document.getElementById('message');
                
                if (response.ok) {
                    messageDiv.className = 'message success';
                    messageDiv.textContent = '保存成功！';
                } else {
                    messageDiv.className = 'message error';
                    messageDiv.textContent = `保存失败：${result.message || '未知错误'}`;
                }
                
                messageDiv.style.display = 'block';
                
                setTimeout(() => {
                    messageDiv.style.display = 'none';
                }, 3000);
            } catch (error) {
                const messageDiv = document.getElementById('message');
                messageDiv.className = 'message error';
                messageDiv.textContent = '保存失败：网络错误';
                messageDiv.style.display = 'block';
            }
        });

        // 页面加载时获取当前配置
        window.addEventListener('load', async function() {
            try {
                const response = await fetch('http://115.190.90.79:58001/api/get');
                const data = await response.json();
                
                if (response.ok) {
                    document.getElementById('url').value = data.url || '';
                    document.getElementById('logo').value = data.logo || '';
                    document.getElementById('title').value = data.title || '';
                }
            } catch (error) {
                console.error('获取配置失败：', error);
            }
        });
    </script>
</body>
</html>
